home *** CD-ROM | disk | FTP | other *** search
- Public Class MenuForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
-
- Friend WithEvents ctxTextBox As System.Windows.Forms.ContextMenu
- Friend WithEvents mnuClearAll As System.Windows.Forms.MenuItem
- Friend WithEvents mnuSelectAll As System.Windows.Forms.MenuItem
- Friend WithEvents mnuTodaysDate As System.Windows.Forms.MenuItem
-
- Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
- Friend WithEvents lblStatus As System.Windows.Forms.Label
-
- Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
-
- Friend WithEvents MenuItem17 As System.Windows.Forms.MenuItem
- Friend WithEvents MenuItem19 As System.Windows.Forms.MenuItem
- Friend WithEvents mnuEditSep2 As System.Windows.Forms.MenuItem
- Friend WithEvents mnuEditSelectAll As System.Windows.Forms.MenuItem
- Friend WithEvents mnuHelp As System.Windows.Forms.MenuItem
- Friend WithEvents mnuHelpSep As System.Windows.Forms.MenuItem
- Friend WithEvents mnuEditSep As System.Windows.Forms.MenuItem
- Friend WithEvents mnuEditCopy As System.Windows.Forms.MenuItem
- Friend WithEvents mnuEditCut As System.Windows.Forms.MenuItem
- Friend WithEvents mnuEdit As System.Windows.Forms.MenuItem
- Friend WithEvents mnuEditUndo As System.Windows.Forms.MenuItem
- Friend WithEvents mnuEditPaste As System.Windows.Forms.MenuItem
- Friend WithEvents mnuFileSave As System.Windows.Forms.MenuItem
- Friend WithEvents mnuFileSaveAs As System.Windows.Forms.MenuItem
- Friend WithEvents mnuFileExit As System.Windows.Forms.MenuItem
- Friend WithEvents mnuFileSep As System.Windows.Forms.MenuItem
- Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
- Friend WithEvents mnuFileNew As System.Windows.Forms.MenuItem
- Friend WithEvents mnuFileOpen As System.Windows.Forms.MenuItem
- Friend WithEvents mnuFileNewDataFile As System.Windows.Forms.MenuItem
- Friend WithEvents mnuFileNewReport As System.Windows.Forms.MenuItem
- Friend WithEvents mnuFileNewFolder As System.Windows.Forms.MenuItem
- Friend WithEvents mnuPopupSep As System.Windows.Forms.MenuItem
- Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
- Friend WithEvents btnCreatePopupMenu As System.Windows.Forms.Button
-
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.MenuItem17 = New System.Windows.Forms.MenuItem()
- Me.mnuFileNewFolder = New System.Windows.Forms.MenuItem()
- Me.MenuItem19 = New System.Windows.Forms.MenuItem()
- Me.TextBox2 = New System.Windows.Forms.TextBox()
- Me.mnuFileSave = New System.Windows.Forms.MenuItem()
- Me.TextBox1 = New System.Windows.Forms.TextBox()
- Me.ctxTextBox = New System.Windows.Forms.ContextMenu()
- Me.mnuClearAll = New System.Windows.Forms.MenuItem()
- Me.mnuSelectAll = New System.Windows.Forms.MenuItem()
- Me.mnuPopupSep = New System.Windows.Forms.MenuItem()
- Me.mnuTodaysDate = New System.Windows.Forms.MenuItem()
- Me.mnuHelp = New System.Windows.Forms.MenuItem()
- Me.mnuHelpSep = New System.Windows.Forms.MenuItem()
- Me.mnuFileNew = New System.Windows.Forms.MenuItem()
- Me.mnuFileNewDataFile = New System.Windows.Forms.MenuItem()
- Me.mnuFileNewReport = New System.Windows.Forms.MenuItem()
- Me.mnuFileExit = New System.Windows.Forms.MenuItem()
- Me.mnuEditCopy = New System.Windows.Forms.MenuItem()
- Me.mnuFileOpen = New System.Windows.Forms.MenuItem()
- Me.mnuEditUndo = New System.Windows.Forms.MenuItem()
- Me.MainMenu1 = New System.Windows.Forms.MainMenu()
- Me.mnuFile = New System.Windows.Forms.MenuItem()
- Me.mnuFileSaveAs = New System.Windows.Forms.MenuItem()
- Me.mnuFileSep = New System.Windows.Forms.MenuItem()
- Me.mnuEdit = New System.Windows.Forms.MenuItem()
- Me.mnuEditSep = New System.Windows.Forms.MenuItem()
- Me.mnuEditCut = New System.Windows.Forms.MenuItem()
- Me.mnuEditPaste = New System.Windows.Forms.MenuItem()
- Me.mnuEditSep2 = New System.Windows.Forms.MenuItem()
- Me.mnuEditSelectAll = New System.Windows.Forms.MenuItem()
- Me.lblStatus = New System.Windows.Forms.Label()
- Me.btnCreatePopupMenu = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'MenuItem17
- '
- Me.MenuItem17.Index = 0
- Me.MenuItem17.Text = "&Index"
- '
- 'mnuFileNewFolder
- '
- Me.mnuFileNewFolder.Index = 2
- Me.mnuFileNewFolder.Text = "&Folder"
- '
- 'MenuItem19
- '
- Me.MenuItem19.Index = 2
- Me.MenuItem19.Text = "&About"
- '
- 'TextBox2
- '
- Me.TextBox2.Location = New System.Drawing.Point(48, 120)
- Me.TextBox2.Name = "TextBox2"
- Me.TextBox2.Size = New System.Drawing.Size(384, 20)
- Me.TextBox2.TabIndex = 3
- Me.TextBox2.Text = ""
- '
- 'mnuFileSave
- '
- Me.mnuFileSave.Index = 2
- Me.mnuFileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS
- Me.mnuFileSave.Text = "&Save"
- '
- 'TextBox1
- '
- Me.TextBox1.ContextMenu = Me.ctxTextBox
- Me.TextBox1.Location = New System.Drawing.Point(48, 72)
- Me.TextBox1.Name = "TextBox1"
- Me.TextBox1.Size = New System.Drawing.Size(384, 20)
- Me.TextBox1.TabIndex = 0
- Me.TextBox1.Text = "Right-click to display context menu"
- '
- 'ctxTextBox
- '
- Me.ctxTextBox.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuClearAll, Me.mnuSelectAll, Me.mnuPopupSep, Me.mnuTodaysDate})
- '
- 'mnuClearAll
- '
- Me.mnuClearAll.Index = 0
- Me.mnuClearAll.Text = "&Clear All"
- '
- 'mnuSelectAll
- '
- Me.mnuSelectAll.Index = 1
- Me.mnuSelectAll.Text = "&Select All"
- '
- 'mnuPopupSep
- '
- Me.mnuPopupSep.Index = 2
- Me.mnuPopupSep.Text = "-"
- '
- 'mnuTodaysDate
- '
- Me.mnuTodaysDate.Index = 3
- Me.mnuTodaysDate.Text = "Insert &Today's Date"
- '
- 'mnuHelp
- '
- Me.mnuHelp.Index = 2
- Me.mnuHelp.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem17, Me.mnuHelpSep, Me.MenuItem19})
- Me.mnuHelp.MergeOrder = 9
- Me.mnuHelp.Text = "&Help"
- '
- 'mnuHelpSep
- '
- Me.mnuHelpSep.Index = 1
- Me.mnuHelpSep.Text = "-"
- '
- 'mnuFileNew
- '
- Me.mnuFileNew.Index = 0
- Me.mnuFileNew.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileNewDataFile, Me.mnuFileNewReport, Me.mnuFileNewFolder})
- Me.mnuFileNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN
- Me.mnuFileNew.Text = "&New"
- '
- 'mnuFileNewDataFile
- '
- Me.mnuFileNewDataFile.Index = 0
- Me.mnuFileNewDataFile.Text = "&Data File"
- '
- 'mnuFileNewReport
- '
- Me.mnuFileNewReport.Index = 1
- Me.mnuFileNewReport.Text = "&Report"
- '
- 'mnuFileExit
- '
- Me.mnuFileExit.Index = 5
- Me.mnuFileExit.Shortcut = System.Windows.Forms.Shortcut.CtrlX
- Me.mnuFileExit.Text = "E&xit"
- '
- 'mnuEditCopy
- '
- Me.mnuEditCopy.Index = 3
- Me.mnuEditCopy.Text = "&Copy"
- '
- 'mnuFileOpen
- '
- Me.mnuFileOpen.Index = 1
- Me.mnuFileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO
- Me.mnuFileOpen.Text = "&Open"
- '
- 'mnuEditUndo
- '
- Me.mnuEditUndo.Index = 0
- Me.mnuEditUndo.Shortcut = System.Windows.Forms.Shortcut.CtrlZ
- Me.mnuEditUndo.Text = "&Undo"
- '
- 'MainMenu1
- '
- Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuEdit, Me.mnuHelp})
- '
- 'mnuFile
- '
- Me.mnuFile.Index = 0
- Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileNew, Me.mnuFileOpen, Me.mnuFileSave, Me.mnuFileSaveAs, Me.mnuFileSep, Me.mnuFileExit})
- Me.mnuFile.MergeType = System.Windows.Forms.MenuMerge.MergeItems
- Me.mnuFile.Text = "&File"
- '
- 'mnuFileSaveAs
- '
- Me.mnuFileSaveAs.Index = 3
- Me.mnuFileSaveAs.Shortcut = System.Windows.Forms.Shortcut.CtrlA
- Me.mnuFileSaveAs.Text = "Save &As"
- '
- 'mnuFileSep
- '
- Me.mnuFileSep.Index = 4
- Me.mnuFileSep.Text = "-"
- '
- 'mnuEdit
- '
- Me.mnuEdit.Index = 1
- Me.mnuEdit.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuEditUndo, Me.mnuEditSep, Me.mnuEditCut, Me.mnuEditCopy, Me.mnuEditPaste, Me.mnuEditSep2, Me.mnuEditSelectAll})
- Me.mnuEdit.MergeOrder = 1
- Me.mnuEdit.Text = "&Edit"
- '
- 'mnuEditSep
- '
- Me.mnuEditSep.Index = 1
- Me.mnuEditSep.Text = "-"
- '
- 'mnuEditCut
- '
- Me.mnuEditCut.Index = 2
- Me.mnuEditCut.Text = "&Cut"
- '
- 'mnuEditPaste
- '
- Me.mnuEditPaste.Index = 4
- Me.mnuEditPaste.Text = "&Paste"
- '
- 'mnuEditSep2
- '
- Me.mnuEditSep2.Index = 5
- Me.mnuEditSep2.Text = "-"
- '
- 'mnuEditSelectAll
- '
- Me.mnuEditSelectAll.Index = 6
- Me.mnuEditSelectAll.Text = "&Select All"
- '
- 'lblStatus
- '
- Me.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
- Me.lblStatus.Dock = System.Windows.Forms.DockStyle.Bottom
- Me.lblStatus.Location = New System.Drawing.Point(0, 217)
- Me.lblStatus.Name = "lblStatus"
- Me.lblStatus.Size = New System.Drawing.Size(480, 24)
- Me.lblStatus.TabIndex = 1
- '
- 'btnCreatePopupMenu
- '
- Me.btnCreatePopupMenu.Location = New System.Drawing.Point(48, 152)
- Me.btnCreatePopupMenu.Name = "btnCreatePopupMenu"
- Me.btnCreatePopupMenu.Size = New System.Drawing.Size(384, 24)
- Me.btnCreatePopupMenu.TabIndex = 2
- Me.btnCreatePopupMenu.Text = "Create Popup Menu for this TextBox"
- '
- 'MenuForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
- Me.ClientSize = New System.Drawing.Size(480, 241)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox2, Me.btnCreatePopupMenu, Me.lblStatus, Me.TextBox1})
- Me.Menu = Me.MainMenu1
- Me.Name = "MenuForm"
- Me.Text = "MenuForm"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- Private Sub MenuForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- InitializeMenuEvents(Me.Menu, AddressOf MenuItem_Click, AddressOf MenuItem_Select)
- End Sub
-
- ' Make all the menu items in this form point to MenuItem_Click and MenuItem_Select routines.
- ' The main application should call this routine with m = Me.Menu
-
- Sub InitializeMenuEvents(ByVal m As Menu, ByVal ClickEvent As EventHandler, ByVal SelectEvent As EventHandler)
- Dim mi As MenuItem
-
- If TypeOf m Is MenuItem Then
- ' Cast the argument to a MenuItem object.
- mi = DirectCast(m, MenuItem)
- ' Initialize the Select event for this menu item.
- AddHandler mi.Select, SelectEvent
- ' Initialize the Click event, but only if this isn't a submenu
- If m.MenuItems.Count = 0 Then
- AddHandler mi.Click, ClickEvent
- End If
- End If
-
- ' Call recursively for all items in the Menuitems collection
- For Each mi In m.MenuItems
- InitializeMenuEvents(mi, ClickEvent, SelectEvent)
- Next
-
- End Sub
-
- ' common Select routine for all menu items
-
- Private Sub MenuItem_Select(ByVal sender As Object, ByVal e As System.EventArgs)
- Dim mi As MenuItem = CType(sender, MenuItem)
- lblStatus.Text = "Help message for " & mi.Text.ToUpper
- End Sub
-
- ' common Click routine for all menu items
-
- Private Sub MenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
- Dim mi As MenuItem = CType(sender, MenuItem)
- lblStatus.Text = mi.Text.ToUpper & " has been selected"
- End Sub
-
- ' implement a Clear command in any control that has the focus
-
- Private Sub mnuClearAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuClearAll.Click
- ' get a reference to parent control
- Dim tb As TextBox = GetSourceTextBox(sender)
- tb.Text = ""
- End Sub
-
- Private Sub mnuTodaysDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuTodaysDate.Click
- ' get a reference to parent control - no need for intermediate variable
- GetSourceTextBox(sender).SelectedText = Now.Today.ToString
- End Sub
-
- ' select the contents of current TextBox control
-
- Private Sub mnuSelectAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSelectAll.Click
- ' get a reference to parent control - no need for intermediate variable
- With GetSourceTextBox(sender)
- .SelectionStart = 0
- .SelectionLength = 9999
- End With
- End Sub
-
- ' Get a reference to the parent control of a context menu's MenuItem
- Function GetSourceTextBox(ByVal sender As Object) As TextBox
- ' cast to a Menuitem object
- Dim mi As MenuItem = CType(sender, MenuItem)
- ' get the parent context menu
- Dim ctxmenu As ContextMenu = DirectCast(mi.Parent, ContextMenu)
- ' Return the source control
- Return DirectCast(ctxmenu.SourceControl, TextBox)
- End Function
-
- ' this event fires when any menu has been activated
-
- Private Sub MenuForm_MenuStart(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MenuStart
- lblStatus.Text = "Select a menu command to see its description"
- End Sub
-
- ' this event fires when the menu has been closed (or a command has been selected)
-
- Private Sub MenuForm_MenuComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MenuComplete
- ' Clear the Label user for help messages.
- lblStatus.Text = ""
- End Sub
-
- ' this command creates a popup menu
-
- Private Sub btnCreatePopupMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreatePopupMenu.Click
- CreateContextMenu()
- End Sub
-
- ' a procedure that actually creates a context menu and
- ' associates it with a TextBox
-
- Sub CreateContextMenu()
- Dim ctxConvertCmds As New ContextMenu()
-
- With ctxConvertCmds.MenuItems
- .Add(New MenuItem("Clear", AddressOf ContextMenu_Click))
- .Add(New MenuItem("Upper Case", AddressOf ContextMenu_Click))
- .Add(New MenuItem("Lower Case", AddressOf ContextMenu_Click))
- End With
- TextBox2.ContextMenu = ctxConvertCmds
- TextBox2.Text = "Right-click to display a popup menu"
- End Sub
-
- ' This procedure reacts to selection in the context menu
-
- Private Sub ContextMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
- Dim mi As MenuItem = DirectCast(sender, MenuItem)
-
- Select Case mi.Text
- Case "Clear"
- TextBox2.Text = ""
- Case "Upper Case"
- TextBox2.Text = TextBox2.Text.ToUpper
- Case "Lower Case"
- TextBox2.Text = TextBox2.Text.ToLower
- End Select
- End Sub
-
- ' this menu fires when the context menu is being displayed
-
- Private Sub ctxTextBox_Popup(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctxTextBox.Popup
- lblStatus.Text = "Context menu is being displayed"
- End Sub
-
- End Class
-